<?php

/*
// EDIT: are these functions only called from admin_ajax.php?
*/

if ( !defined('DOING_AJAX') )
	die();
	
if ( !current_user_can('edit_themes') )
	die('-1');

add_action( 'wp_ajax_BP_delete_theme', 'BP_delete_theme' );
add_action( 'wp_ajax_BP_activate_theme', 'BP_activate_theme' );
add_action( 'wp_ajax_BP_get_content', 'BP_get_content' );
add_action( 'wp_ajax_BP_save_color', 'BP_save_color' );
add_action( 'wp_ajax_BP_delete_color', 'BP_delete_color' );
add_action( 'wp_ajax_BP_save_builder_options', 'BP_save_builder_options' );
add_action( 'wp_ajax_BP_edit_builder_options', 'BP_edit_builder_options' );
add_action( 'wp_ajax_BP_custom_header_upload', 'BP_custom_header_upload' );
add_action( 'wp_ajax_BP_custom_header_crop', 'BP_custom_header_crop' );


function BP_save_color() {
	global $b;
	check_ajax_referer( 'ajaxnonce', '_ajax_save_color' );
	parse_str($_POST['colors'],$colors);
	$lastkey = $b->saveColors("#".$colors['cc_0'],"#".$colors['cc_1'],"#".$colors['cc_2'],"#".$colors['cc_3'],"#".$colors['cc_4'],"#".$colors['cc_5']); //stores the color into db and saves the lastkey
	
	
	if($lastkey) {
		$msg = '<div class="color_scheme_thumb" id="color-scheme-'.$lastkey.'">
            <div class="color_thumb" style="background-color:#'.$colors['cc_0'].'"></div>
            <div class="color_thumb" style="background-color:#'.$colors['cc_1'].'"></div>
			<div class="color_thumb" style="background-color:#'.$colors['cc_2'].'"></div>
			<div class="color_thumb" style="background-color:#'.$colors['cc_3'].'"></div>
			<div class="color_thumb" style="background-color:#'.$colors['cc_4'].'"></div>
			<div class="color_thumb" style="margin-right:20px; background-color:#'.$colors['cc_5'].'"></div>';            
			foreach($colors as $k=>$v)
				$colors[$k] = "#".$v;
				
		$msg .= '<input type="radio" id="colorscheme[]" name="colorscheme" checked="checked"  value="'.$lastkey.'" title="'.implode(",", $colors).'" />&nbsp;<a href="javascript:void(0);" class="delete_color_scheme">[x]</a>';
		$msg .= '<div style="clear:both"></div></div>';
	}
	
	echo $msg;
}

function BP_delete_color() {
	global $b;
	check_ajax_referer( 'ajaxnonce', '_ajax_delete_color' );
	$id = $_POST['id'];		
		if($b->deleteColor($id))
			echo 1;

}


function BP_delete_theme() {
	global $b;
	check_ajax_referer( 'ajaxnonce', '_ajax_delete_theme' );
	$id = $_POST['id'];		
		if($b->deleteTheme($id))
			echo 1;

}

function BP_save_builder_options() {
	global $b;
	check_ajax_referer( 'ajaxnonce', '_ajax_save_builder_options' );
	
	$form1 = array();
	$form2 = array();
	$form3 = array();
	$css = array();
	
	parse_str($_POST['form1'],$form1); //page layout
	parse_str($_POST['form2'],$form2); //page design
	parse_str($_POST['form3'],$form3); //page color
	
	$f2 = explode("&",$_POST['form4']); //page typo
	foreach($f2 as $c)
		parse_str( $c , $css[]);

	
	$themename = $_POST['themename']; //theme name
	$docstyle = $form1['which_doc']; //doc style
	$body = $form1['splitBody-0']; //layout #
	$bannertop = array_key_exists('sidebar_top',$form1)? true : false;  //get bool for banner top
	$bannerbottom = array_key_exists('sidebar_bottom',$form1)? true : false;  //get bool for banner bottom
	
	$stylesheets[] = $form2['header_css'];
	$stylesheets[] = $form2['body_css'];
	$stylesheets[] = $form2['widget_css'];
	$stylesheets[] = $form2['footer_css']; //put all header,footer, content and widget style
	
	$colorscheme = $form3['colorscheme'];	
	
	if($b->checkTheme($themename,false) == true) {
		$b->deleteTheme($b->checkTheme($themename));
	}
	
	if($b->saveTheme($themename,'test',$colorscheme,$bannertop,$bannerbottom,$docstyle,$body,$css,$stylesheets))
		print_r($css);
	else
		echo 'fail';
}

function BP_edit_builder_options() {
	global $b;
	check_ajax_referer( 'ajaxnonce', '_ajax_save_builder_options' );
	
	$form1 = array();
	$form2 = array();
	$form3 = array();
	$css = array();
	
	parse_str($_POST['form1'],$form1); //page layout
	parse_str($_POST['form2'],$form2); //page design
	parse_str($_POST['form3'],$form3); //page color
	
	$f2 = explode("&",$_POST['form4']); //page typo
	foreach($f2 as $c)
		parse_str( $c , $css[]);

	$themename = $_POST['themename']; //theme name
	$docstyle = $form1['which_doc']; //doc style
	$body = $form1['splitBody-0']; //layout #
	$bannertop = array_key_exists('sidebar_top',$form1)? true : false;  //get bool for banner top
	$bannerbottom = array_key_exists('sidebar_bottom',$form1)? true : false;  //get bool for banner bottom
	
	$stylesheets[] = $form2['header_css'];
	$stylesheets[] = $form2['body_css'];
	$stylesheets[] = $form2['widget_css'];
	$stylesheets[] = $form2['footer_css']; //put all header,footer, content and widget style
	
	$colorscheme = $form3['colorscheme'];	
	
	if($b->checkTheme($themename,false) == true) {
		$b->deleteTheme($b->checkTheme($themename));
	}
	
	if($b->editTheme($form1['theme_id'],$themename,'test',$colorscheme,$bannertop,$bannerbottom,$docstyle,$body,$css,$stylesheets))
		echo $theme_id;
	else
		echo 'fail';
}

function BP_activate_theme() {
	global $b;
	check_ajax_referer( 'ajaxnonce', '_ajax_activate_theme' );
	$id = $_POST['id'];
		if($b->activateTheme($id))
			echo 1;

}

function BP_get_content() {
	$type = $_POST['type'];
	
	check_ajax_referer( 'ajaxnonce', '_ajax_get_sidebar' );
	
	switch($type) {
		case '1':
			include( BP_ABSPATH .'/sidebar_1.php' );
		break;
		
		case '2':
			include( BP_ABSPATH .'/sidebar_2.php' );
		break;
		
		case 'top':
			include( BP_ABSPATH .'/sidebar_top.php' );
		break;
		
		case 'bottom':
			include( BP_ABSPATH .'/sidebar_bottom.php' );
		break;
		
		case 'header':
			get_header_content();
		break;
		
		case 'footer':
			get_footer_content();
		break;
		
		case 'content':
			query_posts('showposts=10');
			wp_index();			
		break;
		
		case 'default':
			echo 'default text';
		break;
	}
}


function BP_custom_header_upload() {
	
	$upload_name = "file";
	$max_file_size_in_bytes = 1024*1024;
	$extension_whitelist = array("jpg", "gif", "png");
	
	/* checking extensions */
	$path_info = pathinfo($_FILES[$upload_name]['name']);
	$file_extension = $path_info["extension"];
	$is_valid_extension = false;
	foreach ($extension_whitelist as $extension) {
		if (strcasecmp($file_extension, $extension) == 0) {
			$is_valid_extension = true;
			break;
		}
	}
	if (!$is_valid_extension) {
		echo "{";
		echo		"error: 'Extension not valid'\n";
		echo "}";
		exit(0);
	}
	
	/* file size check */
	$file_size = @filesize($_FILES[$upload_name]["tmp_name"]);
	if (!$file_size || $file_size > $max_file_size_in_bytes) {
		echo "{";
		echo		"error: 'File Exceeds maximum limit'\n";
		echo "}";
		exit(0);
	}
	
	
	if(isset($_FILES[$upload_name]))
		if ($_FILES[$upload_name]["error"] > 0) {
			echo "Error: " . $_FILES["file"]["error"] . "<br />";
			} else  {
				$userfile = stripslashes($_FILES[$upload_name]['name']);
				$file_size = $_FILES[$upload_name]['size'];
				$file_temp = $_FILES[$upload_name]['tmp_name'];
				$file_type = $_FILES[$upload_name]["type"];
				$file_err = $_FILES[$upload_name]['error'];
				$file_name = $userfile;

				if(move_uploaded_file($file_temp, BP_ABSPATH. "/uploads/".$file_name)) {
					echo $file_name;
					exit(0);
				}
			}
	
}


function BP_custom_header_crop() {
		
		$end = BP_ABSPATH . "/uploads/crop.jpg";
		if($end)
			unlink($end);
		
		$jpeg_quality = 90;
		
		$src = BP_ABSPATH . "/uploads/" . $_POST['fname'];
		
		$img_r = imagecreatefromjpeg($src);
		
        $targ_w = $_POST['width'];
        $targ_h = $_POST['height'];
		   
        $dst_r = ImageCreateTrueColor( $targ_w, $targ_h );
        
		imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
		$targ_w,$targ_h,$_POST['w'],$_POST['h']);
	
		imagejpeg($dst_r, $end,$jpeg_quality);
		
		unlink($src);
		
		echo "crop.jpg";
}
